/**
 * Licensed under the Apache License Version 2.0.
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */
package net.interknit.knitter.fw.hook;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.core.SecurityContext;

import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.message.Message;
import org.apache.cxf.phase.AbstractPhaseInterceptor;

public abstract class Hook extends AbstractPhaseInterceptor<Message>
{
	public static final String HTTP_REQUEST = "HTTP.REQUEST";

	public Hook(String phase)
	{
		super(phase);
	}

	protected final HttpServletRequest getRequest(Message message) throws NullPointerException
	{
		HttpServletRequest req = (HttpServletRequest) message.get(HTTP_REQUEST);

		if(req != null) return req;
		else throw new NullPointerException("HttpServletRequest object could not be found in the message");
	}

	protected final SecurityContext getSecurityContext(Message message)
	{
		return message.get(SecurityContext.class);
	}

	@Override
	public void handleMessage(Message message) throws Fault
	{
		System.out.println("Interknit: " + getClass().getName() + ".handleMessage() called");
	}

	@Override
	public void handleFault(Message message)
	{
		super.handleFault(message);

		System.out.println("Interknit: " + getClass().getName() + ".handleFault() called");
	}
}
